package com.cr.front.topic.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cr.common.response.PageData;
import com.cr.front.topic.dao.mapper.TopicMapper;
import com.cr.front.topic.dao.repository.TopicRepository;
import com.cr.front.topic.pojo.po.TopicPO;
import com.cr.front.topic.pojo.query.TopicQuery;
import com.cr.front.topic.pojo.vo.TopicVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class TopicRepositoryImpl implements TopicRepository {
    @Autowired
    private TopicMapper topicMapper;
    @Override
    public List<TopicPO> selectList(TopicQuery topicQuery) {
        QueryWrapper<TopicPO> queryWrapper= new QueryWrapper<>();

        if(topicQuery.getId()!=null){
            queryWrapper.eq("id",topicQuery.getId());
        }
        if(topicQuery.getTopicTitle()!=null){
            queryWrapper.like("topic_title",topicQuery.getTopicTitle());
        }
        if(topicQuery.getContent()!=null){
            queryWrapper.like("content",topicQuery.getContent());
        }
        if(topicQuery.getPage()!=null && topicQuery.getPageSize()!=null){
            queryWrapper.last("LIMIT "+topicQuery.getPageSize()*(topicQuery.getPage()-1)+","+topicQuery.getPageSize());
        }
        List<TopicPO> list = topicMapper.selectList(queryWrapper);

        return list;
    }
    @Override
    public Long selectCount(TopicQuery topicQuery){
        QueryWrapper<TopicPO> queryWrapper= new QueryWrapper<>();
        if(topicQuery.getId()!=null){
            queryWrapper.eq("id",topicQuery.getId());
        }
        if(topicQuery.getTopicTitle()!=null){
            queryWrapper.like("topic_title",topicQuery.getTopicTitle());
        }
        if(topicQuery.getContent()!=null){
            queryWrapper.like("content",topicQuery.getContent());
        }
        return topicMapper.selectCount(queryWrapper);
    }

    @Override
    public TopicPO selectById(Long id) {
        QueryWrapper<TopicPO> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("id",id);
        return topicMapper.selectOne(queryWrapper);

    }
}
